package org.cuijq.thinking.in.spring.ioc.container;

import org.cuijq.thinking.in.spring.ioc.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

import java.util.Map;

/**
 * BeanFactoryAsIocContainerDemo 容器
 */
public class BeanFactoryAsIocContainerDemo {
    public static void main(String[] args) {
        //创建BeanFactory 容器
        DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
        //加载配置
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(defaultListableBeanFactory);
        String localtion = "classpath:/META-INF/dependency-lookuo-context.xml";
        int beansDefinitionCounts =  reader.loadBeanDefinitions(localtion);
        System.out.println(beansDefinitionCounts);
        lookupByCollectType(defaultListableBeanFactory);
    }

    private static void lookupByCollectType(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory){
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String , User> users=listableBeanFactory.getBeansOfType(User.class);
            System.out.println("查找所有的User 对象"+users);
        }
    }
}
